package com.example.dw.admin.service.impl;

import com.example.dw.admin.dto.PageRequestDTO;
import com.example.dw.admin.dto.CreateNewsDTO;
import com.example.dw.admin.dto.UpdateNewsDTO;
import com.example.dw.admin.service.AdminNewsService;
import com.example.dw.entity.News;
import com.example.dw.mapper.NewsMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理端新闻服务实现类
 * @author system
 * @since 2024-01-01
 */
@Slf4j
@Service
public class AdminNewsServiceImpl implements AdminNewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public List<News> getSomeNews(Integer count) {
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("createdAt")
                   .last("LIMIT " + count);
        return newsMapper.selectList(queryWrapper);
    }

    @Override
    public Map<String, Object> getNewsByPage(PageRequestDTO pageRequest) {
        Page<News> page = new Page<>(pageRequest.getPage(), pageRequest.getSize());
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        
        // 添加搜索条件
        if (StringUtils.hasText(pageRequest.getKeyword())) {
            queryWrapper.and(wrapper -> wrapper
                    .like("newsName", pageRequest.getKeyword())
                    .or()
                    .like("newsContent", pageRequest.getKeyword())
                    .or()
                    .like("newsPeople", pageRequest.getKeyword())
            );
        }
        
        // 按创建时间倒序
        queryWrapper.orderByDesc("createdAt");
        
        IPage<News> newsPage = newsMapper.selectPage(page, queryWrapper);
        
        Map<String, Object> result = new HashMap<>();
        result.put("records", newsPage.getRecords());
        result.put("total", newsPage.getTotal());
        result.put("current", newsPage.getCurrent());
        result.put("size", newsPage.getSize());
        result.put("pages", newsPage.getPages());
        
        return result;
    }

    @Override
    public News getNewsById(Integer newsid) {
        return newsMapper.selectById(newsid);
    }

    @Override
    public void createNews(CreateNewsDTO createNewsDTO) {
        News news = new News();
        BeanUtils.copyProperties(createNewsDTO, news);
        
        // 设置创建时间和发布时间
        LocalDateTime now = LocalDateTime.now();
        news.setCreatedAt(now);
        news.setNewsDate(now);
        
        int result = newsMapper.insert(news);
        if (result <= 0) {
            throw new RuntimeException("创建新闻失败");
        }
    }

    @Override
    public void updateNews(UpdateNewsDTO updateNewsDTO) {
        // 检查新闻是否存在
        News existingNews = newsMapper.selectById(updateNewsDTO.getNewsid());
        if (existingNews == null) {
            throw new RuntimeException("新闻不存在，无法更新");
        }
        
        News news = new News();
        BeanUtils.copyProperties(updateNewsDTO, news);
        
        int result = newsMapper.updateById(news);
        if (result <= 0) {
            throw new RuntimeException("更新新闻失败");
        }
    }

    @Override
    public void deleteNews(Integer newsid) {
        // 检查新闻是否存在
        News existingNews = newsMapper.selectById(newsid);
        if (existingNews == null) {
            throw new RuntimeException("新闻不存在，无法删除");
        }
        
        int result = newsMapper.deleteById(newsid);
        if (result <= 0) {
            throw new RuntimeException("删除新闻失败");
        }
    }
} 